define('controllers/map', function(require, exports, module) {
    "use strict";
    var fnGlobalUrl = require[require.toUrl ? 'toUrl' : 'resolve'];
    var $window = $(window),
        $document = $(document);
    /*var m_Swiper = require('swiper');*/ //引用一个js插件实例
    /* ************************************************************ */
    var m_MapPage = { /* 定义一个首页对象 */
        createNew: function() {
            if (!(window.basePage) || typeof(window.basePage.createNew) !== 'function') {
                window.basePage = {};
                window.basePage.createNew = function() {
                    return {};
                }
            }
            var $internal = window.basePage.createNew();
            /* code here for example */
            $internal.mapData = null;
            $internal.init = function() { /* 页面类的初始化， 向$internal上绑定功能对象， 如下绑定 Banner 对象 */
                /*code here 如下： */
                this.mapData = new $mapData();
            };
            var $mapData = new Class({ /* 创建banner对象 */
                initialize: function() {
                    this._init();
                }
            });
            $mapData.extend({ /* 向banner对象添加 banner的方法  */
                _init: function() {
                    this.isAppear = false; /* 是否呈现完毕标志 */
                    this.mySwiper = null;
                    this.myTimers = undefined; /* 预定义定时器  */
                    this.map = undefined;
                    this.mapData = '';
                    this.areaId = 0;
                    this.flag = 0;
                },
                appear: function() { /* 呈现相关逻辑  如swiper 的配置和和绑定等 */
                    var _this = this,
                        defaultX = 102.722061,
                        defaultY = 25.048688,
                        defaultPoint = new BdMap.Point(defaultX, defaultY);
                    _this.map = new BdMap.Map("mymap", {
                        minZoom: 11,
                        maxZoom: 17
                    }); // 创建Map实例
                    _this.map.centerAndZoom(defaultPoint, 13); // 初始化地图,设置中心点坐标和地图级别
                    _this.map.setCurrentCity("昆明"); // 设置地图显示的城市 此项是必须设置的
                    _this.map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
                    // 获取请求数据
                    var data = [],
                        mapUrl = $('.J_map_form').attr('ajaxUrl');
                    $('.J_map_form').find('input[type="text"]').each(function(num, dom) {
                        var _nhe = $(this),
                            nName = _nhe.prop('name'),
                            nVal = _nhe.attr('data-type') || _nhe.val() || '';
                        data[num] = eval('({"name":"' + nName + '","value":"' + nVal + '"})');
                    });
                    _this.mapAjax(data, mapUrl, 1);
                    _this.evetFun(); // 全部地图交互事件
                },
                resetFlag: function() {
                    var _this = this;
                    _this.flag = 0;
                },
                evetFun: function() {
                    var _this = this;
                    // 监听缩放结束事件
                    _this.map.addEventListener("zoomend", function() {
                        if (this.getZoom() > 14) {
                            for (var i = 0; i < _this.mapData.area.length; i++) {
                            	if(_this.mapData.area[i].tapObj.hide){
                                _this.mapData.area[i].tapObj.hide();
                            	}
                            }
                            for (var i = 0; i < _this.mapData.school.length; i++) {
                            	if(_this.mapData.school[i].tapObj.show){
                                _this.mapData.school[i].tapObj.show();
                            	}
                            }
                        } else if (this.getZoom() <= 14 && this.getZoom() >= 12) {
                            for (var i = 0; i < _this.mapData.area.length; i++) {
                            	if(_this.mapData.area[i].tapObj.show){
                                _this.mapData.area[i].tapObj.show();
                            	}
                            }
                            for (var i = 0; i < _this.mapData.school.length; i++) {
                            	if(_this.mapData.school[i].tapObj.hide){
                                _this.mapData.school[i].tapObj.hide();
                            	}
                            }
                        } else if (this.getZoom() < 12) {
                            for (var i = 0; i < _this.mapData.area.length; i++) {
                            	if(_this.mapData.area[i].tapObj.small){
                                _this.mapData.area[i].tapObj.small();
                              }
                            }
                            for (var i = 0; i < _this.mapData.school.length; i++) {
                            	if(_this.mapData.school[i].tapObj.hide){
                                _this.mapData.school[i].tapObj.hide();
                            	}
                            }
                        }
                        // _this.getList();
                    });
                    // 监听地图位置变动结束事件
                    // _this.map.addEventListener("moveend", function() {
                    //     _this.getList();
                    // });
                    // 监听鼠标滚轮事件
                    // var scrollFunc=function(e){
                    // 	 console.log(_this.flag);
                    //   e=e || window.event;
                    //   var newZoom=_this.map.getZoom();
                    //   if(e.wheelDelta>0&&!_this.flag){
                    //      _this.flag=1;
                    //   	  newZoom = newZoom + 2;
                    //    	_this.map.setZoom(newZoom);
                    //     setTimeout(_this.resetFlag(),1200);
                    //   }else if(e.wheelDelta<0&&!_this.flag){
                    //     _this.flag=1;
                    //   	 newZoom = newZoom - 2;
                    //   	 _this.map.setZoom(newZoom);
                    //     setTimeout(_this.resetFlag(),1200);
                    //   }
                    // }
                    // if(document.addEventListener){
                    //   document.addEventListener('DOMMouseScroll',scrollFunc,false);
                    // }//W3C
                    // window.onmousewheel=document.onmousewheel=scrollFunc;//IE/Opera/Chrome
                    // 下拉选择事件
                    $('.J_map_form').on('click', '.J_a', function() {
                        var _the = $(this),
                            _theTxt = _the.text(),
                            _theVal = _the.attr('data-type') || _the.text();
                        _the.parents('.map_select').find('.map_select_on').val(_theTxt).attr('data-type', _theVal);
                        var data = [],
                            mapUrl = $('.J_map_form').attr('ajaxUrl');
                        $('.J_map_form').find('input[type="text"]').each(function(num, dom) {
                            var _nhe = $(this),
                                nName = _nhe.prop('name'),
                                nVal = _nhe.attr('data-type') || _nhe.val() || '';
                            data[num] = eval('({"name":"' + nName + '","value":"' + nVal + '"})');
                        });
                        _this.mapAjax(data, mapUrl);
                    });
                    $('.J_map_form').on('click', '.J_map_submit', function() {
                        var data = [],
                            mapUrl = $('.J_map_form').attr('ajaxUrl');
                        $('.J_map_form').find('input[type="text"]').each(function(num, dom) {
                            var _nhe = $(this),
                                nName = _nhe.prop('name'),
                                nVal = _nhe.attr('data-type') || _nhe.val() || '';
                            data[num] = eval('({"name":"' + nName + '","value":"' + nVal + '"})');
                        });
                        _this.mapAjax(data, mapUrl);
                    });
                    // 圆点击事件
                    $('body').on('mousedown','.mytap',function(ev) {
                        _this.map.disableDragging();
                        var oEvent = ev||event,
                            newZoom = 13,
                            oldPpint = _this.map.getZoom(),
                            lng = $(this).attr('data-lng'),
                            lat = $(this).attr('data-lat'),
                            newPoint = new BdMap.Point(lng, lat);
                        newZoom = oldPpint + 2;
                        _this.map.setZoom(newZoom);
                        _this.map.panTo(newPoint);
                        oEvent.cancelBubble = true;
                        oEvent.stopPropagation();
                        setTimeout(function(){_this.map.enableDragging();},1000);
                    });
                    $('body').on('click', '.J_list .item', function() {
                        var newZoom = 17,
                            lng = $(this).attr('data-lng'),
                            lat = $(this).attr('data-lat'),
                            newPoint = new BdMap.Point(lng, lat);
                        _this.map.setZoom(newZoom);
                        _this.map.panTo(newPoint);
                    });
                },
                // 数据请求
                mapAjax: function(data, ajaxUrl, first) {
                    $('#map_main').show();
                    var _this = this,
                        flag = 0;
                    if (first) {
                        var _theFirst = {};
                        _theFirst.name = "first";
                        _theFirst.value = "true";
                        data.push(_theFirst);
                    }
                    if (!flag) {
                        flag = 1;
                        layer.load(1, {
                            shade: [0.6, '#000']
                        });
                        $.ajax({
                            url: ajaxUrl,
                            type: 'POST',
                            data: data || '',
                        }).done(function(res) {
                            res = eval(res);
                            if (res.code == 200) {
                                
                                if (first) {
                                    _this.mapData = res.data;
                                    _this.getArea();
                                    _this.getSchool();
                                    _this.getList();
                                } else if (!first && res.data.area && res.data.school) {
                                    _this.mapData.school = res.data.school;
						                        $('.mytap').each(function() {
						                            var _the = $(this),
						                                _theName = _the.find('h3').text(),
						                                newNum = 0;
						                            for (var i = 0; i < res.data.area.length; i++) {
						                                if (_theName == res.data.area[i].name) {
						                                    _the.find('p strong').text(res.data.area[i].count);
						                                    newNum = res.data.area[i].count;
						                                }
						                            }
		                                    for(var i = 0; i < res.data.area.length; i++){
		                                    	if(_theName == _this.mapData.area[i].name){
		                                    		_this.mapData.area[i].count = newNum;
		                                    	}
		                                    }
						                        });
                                    _this.getSchool(1);
                                    _this.getList();
                                }
                                if (res.data.link) {
                                    setTimeout(function() {
                                        if (res.data.link.size) {
                                            _this.map.setZoom(17);
                                        } else {
                                            _this.map.setZoom(13);
                                        };
                                        _this.map.panTo(new BdMap.Point(res.data.link.lng, res.data.link.lat));
                                    }, 1000);
                                }
                            }
                        }).fail(function() {}).always(function() {
                            layer.closeAll('loading');
                            flag = 0;
                        });
                    }
                },
                // 创建左侧列表
                foundList: function(data, jud) {
                    var _this = this,
                        _theList = jQuery('.J_list'),
                        newItem = jQuery('<li class="item"></li>'),
                        domModel = jQuery('.J_dom').html();
                    newItem.html(domModel);
                    newItem.find('img').attr('src', data.imgUrl);
                    newItem.find('h3').text(data.name);
                    newItem.find('p').append(data.address);
                    newItem.attr('data-lat', data.lat);
                    newItem.attr('data-lng', data.lng);
                    if (!jud) {
                        _theList.html(newItem);
                    } else {
                        _theList.append(newItem);
                    }
                },
                // 获取左侧列表学校
                getList: function() {
                    var _this = this;
                    var first = 0;
                    var bs = _this.map.getBounds(); //获取可视区域
                    var bssw = bs.getSouthWest(); //可视区域左下角
                    var bsne = bs.getNorthEast(); //可视区域右上角
                    // console.log('左下==='+bssw.lat+'：'+bssw.lng);
                    // console.log('右上==='+bsne.lat+'：'+bsne.lng);
                    for (var i = 0; i < _this.mapData.school.length; i++) {
                        // var _theLat = _this.mapData.school[i].lat,
                        //     _theLng = _this.mapData.school[i].lng,
                        //     jud = (bssw.lng < _theLng) && (bssw.lat < _theLat) && (bsne.lng > _theLng) && (bsne.lat > _theLat);
                        _this.foundList(_this.mapData.school[i], first);
                        first = 1;
                        // if (jud && !first) {
                        //     _this.foundList(_this.mapData.school[i], first);
                        //     first = 1;
                        // } else if(jud && first) {
                        //     _this.foundList(_this.mapData.school[i], first);
                        // }
                    }
                    if (!first) {
                        jQuery('.J_list').html('<li class="noHas">该区域没有发现有关内容</li>')
                    }
                },
                // 创建学校坐标
                foundSchool: function(data, index) {
                    var _this = this,
                        newSchool = function(data) {
                            this.seat = {};
                            this.seat.lat = data.lat;
                            this.seat.lng = data.lng;
                            this.name = data.name;
                            this.url = '/school/web/school-detail?&id=' + data.id;
                        };
                    newSchool.prototype = new BdMap.Overlay();
                    newSchool.prototype.initialize = function() {
                        var newDiv = document.createElement("div"),
                            newIco = document.createElement("i"),
                            newTit = document.createElement("h3"),
                            newUrl = document.createElement("a");
                        this._map = _this.map;
                        newIco.className = 'fa fa-map-marker';
                        newDiv.className = 'mylabel fn-dn';
                        newUrl.appendChild(document.createTextNode(this.name));
                        newUrl.setAttribute("href", this.url);
                        newTit.appendChild(newUrl);
                        newDiv.appendChild(newIco);
                        newDiv.appendChild(newTit);
                        newDiv.setAttribute('data-lat', data.lat);
                        newDiv.setAttribute('data-lng', data.lng);
                        _this.map.getPanes().markerPane.appendChild(newDiv);
                        this.domObj = newDiv;
                        return newDiv;
                    };
                    newSchool.prototype.draw = function() {
                        var position = _this.map.pointToOverlayPixel(this.seat); // 根据地理坐标转换为像素坐标
                        this.domObj.style.left = position.x + 'px';
                        this.domObj.style.top = position.y + 'px';
                    };
                    newSchool.prototype.show = function() {
                        if (this.domObj) {
                            this.domObj.style.display = "block";
                        }
                    };
                    newSchool.prototype.hide = function() {
                        if (this.domObj) {
                            this.domObj.style.display = "";
                        }
                    };
                    _this.mapData.school[index].tapObj = new newSchool(data);
                    _this.map.addOverlay(_this.mapData.school[index].tapObj);
                },
                // 创建区域坐标
                foundArea: function(data, index) {
                    var _this = this,
                        newArea = function(data) {
                            this.seat = {};
                            this.seat.lat = data.lat;
                            this.seat.lng = data.lng;
                            this.name = data.name;
                            this.count = data.count;
                        };
                    newArea.prototype = new BdMap.Overlay();
                    newArea.prototype.initialize = function() {
                        var newDiv = document.createElement("div"),
                            newIco = document.createElement("i"),
                            newTit = document.createElement("h3"),
                            newTxt = document.createElement("p"),
                            newNum = document.createElement("strong");
                        this._map = _this.map;
                        newIco.className = 'fa fa-map-marker';
                        newDiv.className = 'mytap big';
                        newTit.appendChild(document.createTextNode(this.name));
                        newNum.appendChild(document.createTextNode(this.count));
                        newTxt.appendChild(document.createTextNode('共'));
                        newTxt.appendChild(newNum);
                        newTxt.appendChild(document.createTextNode('所学校'));
                        newDiv.appendChild(newIco);
                        newDiv.appendChild(newTit);
                        newDiv.appendChild(newTxt);
                        newDiv.setAttribute('data-lat', data.lat);
                        newDiv.setAttribute('data-lng', data.lng);
                        _this.map.getPanes().markerPane.appendChild(newDiv);
                        this.domObj = newDiv;
                        return newDiv;
                    };
                    newArea.prototype.draw = function() {
                        var position = _this.map.pointToOverlayPixel(this.seat); // 根据地理坐标转换为像素坐标
                        this.domObj.style.left = position.x + 'px';
                        this.domObj.style.top = position.y + 'px';
                    };
                    newArea.prototype.show = function() {
                        if (this.domObj) {
                            this.domObj.style.display = "";
                            this.domObj.className = "mytap big";
                        }
                    };
                    newArea.prototype.small = function() {
                        if (this.domObj) {
                            this.domObj.style.display = "";
                            this.domObj.className = "mytap small";
                        }
                    };
                    newArea.prototype.hide = function() {
                        if (this.domObj) {
                            this.domObj.style.display = "none";
                        }
                    };
                    _this.mapData.area[index].tapObj = new newArea(data);
                    _this.map.addOverlay(_this.mapData.area[index].tapObj);
                },
                // 获取学校数据
                getSchool: function(jud) {
                    // console.log(data);
                    var _this = this;
                    if (jud) {
                        $('.mylabel').remove();
                    }
                    for (var i = 0; i < _this.mapData.school.length; i++) {
                        _this.foundSchool(_this.mapData.school[i], i);
                    }
                    // _this.getList();
                },
                // 获取区域数据
                getArea: function() {
                    var _this = this;
                    for (var i = 0; i < _this.mapData.area.length; i++) {
                        _this.foundArea(_this.mapData.area[i], i);
                    }
                    _this.boundary();
                },
                // 区块显示隐藏
                boundary: function() {
                    var _this = this;
                    $('body').find('.mytap').each(function() {
                        var _the = this,
                            _theName = jQuery(_the).find('h3').text(),
                            _theBdary = new BdMap.Boundary();
                        _theBdary.get(_theName, function(rs) {
                            var list = [];
                            for (var i = 0; i < rs.boundaries.length; i++) {
                                list[i] = {};
                                list[i].block = new BdMap.Polygon(rs.boundaries[i], {
                                    strokeWeight: 2,
                                    strokeColor: "#f86d3b",
                                    strokeOpacity: 0.8,
                                    fillColor: "#f86d3b",
                                    fillOpacity: 0.2
                                }); //建立多边形覆盖物
                                _this.map.addOverlay(list[i].block);
                                list[i].block.hide();
                            }
                            $(_the).on('mouseover mouseout', function(event) {
                                if (event.type == 'mouseover') {
                                    for (var i = 0; i < list.length; i++) {
                                        list[i].block.show();
                                    };
                                } else if (event.type == 'mouseout') {
                                    for (var i = 0; i < list.length; i++) {
                                        list[i].block.hide();
                                    };
                                }
                            });
                        });
                    });
                },
            });
            return $internal; //返回对象
        }
    };
    exports.run = function() {
        var MAPPAGE = m_MapPage.createNew(); /* 定义首页页面类 */
        MAPPAGE.init(); /* 调用首页页面类入口方法  */
        $document.ready(function() {
            MAPPAGE.mapData.appear(); /* 当dom 加载完成后调用 banner 的呈现函数 */
        });
    };
});